﻿using System;
using System.Windows.Forms;
using KZ.Exception;
using KZ.Lottery.BLL;
using KZ.Lottery.Resources;
using KZ.Utilities;

namespace KZ.Lottery
{
    public partial class FrmRule : Form
    {
        #region Private Variables

        int _PreviousRowIndex;

        #endregion Private Variables

        #region Private Methods

        private void FrmRule_Load(object sender, EventArgs e)
        {
            LoadRule();
            dtgRule.SelectionChanged += dtgRule_SelectionChanged;
        }

        private void LoadRuleDetail(int rowIndex)
        {
            try
            {
                string ruleID = Utility.StringParse(dtgRule[colRuleID.Index, rowIndex].Value);

                dtgRuleDetail.DataSource = null;
                dtgRuleDetail.DataSource = RuleDetailBLL.GetRuleDetailListWithChannel(ruleID);

                _PreviousRowIndex = rowIndex;
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadRuleDetail", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        private void dtgRule_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                string ruleID = Utility.StringParse(dtgRule[colRuleID.Index, e.RowIndex].Value);
                string ruleName = Utility.StringParse(dtgRule[colRuleName.Index, e.RowIndex].Value);
                Global.OpenTab(new FrmRuleDetailGroup
                {
                    RuleID = ruleID,
                    Name = "rule" + ruleID,
                    Text = "Quy luật: " + ruleName
                });
            }
        }

        private void dtgRule_SelectionChanged(object sender, EventArgs e)
        {
            LoadRuleDetail(dtgRule.CurrentRow.Index);
        }

        private void dtgRule_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && dtgRule.SelectedRows.Count > 0)
            {
                tsmiDelete_Click(tsmiDelete, null);
            }
        }

        private void contextMenuStrip1_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            tsmiUpdate.Enabled = dtgRule.RowCount > 0;
            tsmiDelete.Enabled = dtgRule.RowCount > 0;
            tsmiViewRuleDetailGroup.Enabled = dtgRule.RowCount > 0;
        }

        private void tsmiInsert_Click(object sender, EventArgs e)
        {
            FrmRuleInsert frm = new FrmRuleInsert();
            frm.Action = KZ_Resource.Action_Insert;
            frm.ShowDialog();
        }

        private void tsmiUpdate_Click(object sender, EventArgs e)
        {
            FrmRuleInsert frm = new FrmRuleInsert();
            frm.RuleID = Utility.StringParse(dtgRule[colRuleID.Index, dtgRule.CurrentCell.RowIndex].Value);
            frm.Action = KZ_Resource.Action_Update;
            frm.ShowDialog();
        }

        private void tsmiCopy_Click(object sender, EventArgs e)
        {
            FrmRuleInsert frm = new FrmRuleInsert();
            frm.RuleID = Utility.StringParse(dtgRule[colRuleID.Index, dtgRule.CurrentCell.RowIndex].Value);
            frm.Action = KZ_Resource.Action_Copy;
            frm.ShowDialog();
        }

        private void tsmiDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (DialogResult.Yes == MessageBox.Show(KZ_Message.BLM_CFM_DELETE, String.Empty, MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    string id = Global.GetDtgValue(dtgRule, colRuleID.Index);
                    if (RuleBLL.DeleteRule(id))
                    {
                        LoadRule();
                        Global.SetMessage(lblMessage, KZ_Message.BLM_SUC_RULE_DELETE, true);
                    }
                }
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("tsmiDelete_Click", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        private void tsmiActivate_Click(object sender, EventArgs e)
        {
            try
            {
                if (DialogResult.Yes == MessageBox.Show(KZ_Message.BLM_CFM_ACTIVATE, String.Empty, MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    string id = Global.GetDtgValue(dtgRule, colRuleID.Index);
                    if (RuleBLL.ActivateRule(id))
                    {
                        LoadRule();
                        Global.SetMessage(lblMessage, KZ_Message.BLM_SUC_RULE_ACTIVATE, true);
                    }
                }
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("tsmiDelete_Click", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        private void tsmiInactivate_Click(object sender, EventArgs e)
        {
            try
            {
                if (DialogResult.Yes == MessageBox.Show(KZ_Message.BLM_CFM_INACTIVATE, String.Empty, MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    string id = Global.GetDtgValue(dtgRule, colRuleID.Index);
                    if (RuleBLL.InactivateRule(id))
                    {
                        LoadRule();
                        Global.SetMessage(lblMessage, KZ_Message.BLM_SUC_RULE_INACTIVATE, true);
                    }
                }
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("tsmiDelete_Click", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        private void tsmiViewRuleDetailGroup_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dtgRule.SelectedRows)
            {
                string ruleID = Utility.StringParse(row.Cells[colRuleID.Index].Value);
                string ruleName = Utility.StringParse(row.Cells[colRuleName.Index].Value);
                Global.OpenTab(new FrmRuleDetailGroup
                {
                    RuleID = ruleID,
                    Name = "rule" + ruleID,
                    Text = "Quy luật: " + ruleName
                });
            }
        }

        #endregion Private Methods

        #region Constructors

        public FrmRule()
        {
            InitializeComponent();
            dtgRule.AutoGenerateColumns = false;
            dtgRuleDetail.AutoGenerateColumns = false;
        }

        #endregion Constructors

        #region Public Members

        #endregion Public Members

        #region Public Memthods

        public void LoadRule()
        {
            try
            {
                dtgRule.DataSource = null;
                dtgRule.DataSource = RuleBLL.GetRuleList();
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadRule", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        #endregion Public Memthods
    }
}